#include <iostream>
#include "lmanagerview_console_impl.h"
#include "lmanager.h"
#include "lmanagerservice.h"
#include "lmanagerservice_impl.h"
#include "tools.h"
#include <getch.h>
using namespace std;
using namespace MC;


LmanagerviewConsoleImpl::LmanagerviewConsoleImpl(void)
{
	service = new LmanagerServiceImpl;
}

void LmanagerviewConsoleImpl::menu(void)
{
	while(1)
	{
		system("clear");
		cout << "*******欢迎来到图书管理系统*******" << endl;
		cout << "       1、添加新管理员" << endl;
		cout << "       2、删除管理员  " << endl;
		cout << "       3、列出所有管理员名单" << endl;
		cout << "       4、退出root" << endl;
		cout << "*********************************" << endl;
		cout << "请输入指令：";
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='4')
				break;
		}
		cout << cmd << endl;
		switch(cmd-'0')
		{
			case 1:add();break;
			case 2:del();break;
			case 3:list();break;
			case 4:return;
		}
	}	
}
void LmanagerviewConsoleImpl::add(void)
{
	int m_id = generator_MgrId();
	char m_name[20];
	char m_password[20];
	cout << "请输入管理员名：" << endl;
	cin >> m_name;
	cout << "请输入密码：" << endl;
	cin >> m_password;
	Lmanager man(m_id++,m_name,m_password);
	
	if(service->addManager(man))
	{
		cout << "管理员添加成功！" << endl;
	}
	else
	{
		cout << "添加失败!" << endl;
	}
	anykey_continue();

}
void LmanagerviewConsoleImpl::del(void)
{
	int _id = generator_MgrId();
	cout << "请输入要删除管理员ID：" << endl;
	cin >> _id;
	if(service->deleteManager(_id))
	{
		cout << "删除管理员成功！" << endl;
	}
	else
	{
		cout << "删除失败!" << endl;
		
	}
	anykey_continue();

}
void LmanagerviewConsoleImpl::list(void)
{
	service->listManager();
	anykey_continue();
}
LmanagerviewConsoleImpl::~LmanagerviewConsoleImpl(void)
{
	delete []service;
}
